#include "screenwidget.h"
screenWidget::screenWidget(QWidget *parent) : QWidget(parent)
{
    isMousePress=false;
    beginPoint=QPoint(-1,-1);
    endPoint=beginPoint;
}

void screenWidget::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.setPen(QPen(Qt::green,2,Qt::DotLine,Qt::FlatCap));
    painter.drawRect(10,10,100,100);
    painter.drawPixmap(0,0,totalScreen);
    painter.fillRect(this->rect(),QColor(0,0,0,100));
    cutScreen=totalScreen;
    if(isMousePress){
        QRect selectRect;
        beginPoint.x()>endPoint.x()?selectRect=QRect(endPoint,beginPoint):selectRect=QRect(beginPoint,endPoint);
        cutScreen=totalScreen.copy(selectRect);
        painter.drawPixmap(selectRect.topLeft(),cutScreen);
        painter.drawRect(selectRect);
    }

}

void screenWidget::mousePressEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton){
        isMousePress=true;
        beginPoint=e->pos();
    }else if(e->button()==Qt::RightButton){//右键取消截图
        this->close();
    }

}

void screenWidget::mouseMoveEvent(QMouseEvent *e)
{
    if(isMousePress){
        endPoint=e->pos();
        update();
}
}

void screenWidget::mouseReleaseEvent(QMouseEvent *e)
{
    endPoint=e->pos();
    isMousePress=false;
    //update();
}

void screenWidget::keyPressEvent(QKeyEvent *e)
{
    if(e->key()==Qt::Key_Return){//Key_Return是小数字键盘的enter
        emit cut(cutScreen);
        close();
    }

}




